import 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';

/*

https://pub-web.flutter-io.cn/packages/flutter_keychain


加密 ANDROID IOS LINUX MACOS WEB WINDOWS
https://pub.flutter-io.cn/packages/crypto
SHA-1
SHA-224
SHA-256
SHA-384
SHA-512
SHA-512/224
SHA-512/256
MD5
HMAC (i.e. HMAC-MD5, HMAC-SHA1, HMAC-SHA256)

dependencies:
  crypto: ^3.0.2
import 'package:crypto/crypto.dart';



其他:
https://pub.flutter-io.cn/packages/encrypt
https://pub.flutter-io.cn/packages/jwt_decoder

 */
abstract class XJiamiUtils{


  ///使用md5加密
  static String strToMd5(String? data) {

    if(XStringUtils.isEmpty(data)){
      return '';
    }

    //Uint8List content = const Utf8Encoder().convert(data);
    Uint8List content = XStringUtils.stringToUint8List_utf8(data!);
    Digest digest = md5.convert(content);
    return digest.toString();
  }


  ///使用md5加密
  static String strToSHA(String? data,{
    String type='sha256',
    String? privateKey,
  }) {

    if(XStringUtils.isEmpty(data)){
      return '';
    }

    //Uint8List content = const Utf8Encoder().convert(data);
    Uint8List content = XStringUtils.stringToUint8List_utf8(data!);

    Digest digest;

    if(type=='sha1'){
      if(XStringUtils.isEmpty(privateKey)){
        digest = sha1.convert(content);
      }else{
        digest=Hmac(sha1, utf8.encode(privateKey!)).convert(content);
      }
    }else if(type=='sha256'){
      if(XStringUtils.isEmpty(privateKey)){
        digest = sha256.convert(content);
      }else{
        digest=Hmac(sha256, utf8.encode(privateKey!)).convert(content);
      }
    }else if(type=='sha384'){
      if(XStringUtils.isEmpty(privateKey)){
        digest = sha384.convert(content);
      }else{
        digest=Hmac(sha384, utf8.encode(privateKey!)).convert(content);
      }
    }else if(type=='sha512'){
      if(XStringUtils.isEmpty(privateKey)){
        digest = sha512.convert(content);
      }else{
        digest=Hmac(sha512, utf8.encode(privateKey!)).convert(content);
      }
    }else if(type=='md5'){
      if(XStringUtils.isEmpty(privateKey)){
        digest = md5.convert(content);
      }else{
        digest=Hmac(md5, utf8.encode(privateKey!)).convert(content);
      }
    }else{
      //默认sha256
      if(XStringUtils.isEmpty(privateKey)){
        digest = sha256.convert(content);
      }else{
        digest=Hmac(sha256, utf8.encode(privateKey!)).convert(content);
      }
    }

    //
    return digest.toString();
  }







  //base64/////////////////////////////////////////

  //String转base64 (utf8)
  static String strToBase64(String text){
    final String contentBase64 = base64Encode(
      const Utf8Encoder().convert(text),
    );
    return contentBase64;
  }


  //base64转String
  static String base64ToString(String base64Str){
    final String content = const Utf8Decoder().convert(base64Decode(base64Str));
    return content;
  }

  /*
  base64转bytes
  注意:android的byte是8位的(-128~127), flutter没有byte只有int,因此没有负数, 在flutter的List<int>中,>=128的数,减256后即为对应java的byte数据,
  因此base64转List<int>或String转转List<int>后的数据值是没有问题的,可直接写入
  */
  static Uint8List base64ToUint8List(String base64Str){
    //等同  base64.decode(base64Str); base64.decoder.convert(base64Str);
    return base64Decode(base64Str);
  }

  static String uint8ListToBase64(Uint8List list){
    //等同 base64.decode(base64Str); base64.decoder.convert(base64Str);
    return base64Encode(list);
  }


  ///////////////////////////////////////////////////
  //加密url,避免中文乱码
  static String encodeUrl(String url){
    return Uri.encodeComponent(url);
  }
  //解密url,避免中文乱码
  static String decodeUrl(String url){
    return Uri.decodeComponent(url);
  }




}
